//
//  HBHTTPClient.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/23.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit
import AFNetworking

enum ClientType: String {
    case GET = "GET"
    case POST = "POST"
}

class HBHTTPClient: AFHTTPSessionManager {
    
    // 可以在这里创建单例类, 利于管理
    static let shareInstance: HBHTTPClient = {
        let client = HBHTTPClient()
        //
        client.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return client
    }()

    // 把 init方法设置成private
    private init() {
        // 必须要调用指定的构造函数
        super.init(baseURL: nil, sessionConfiguration: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /* 网络请求类: GET & POST */

    /*
    override func GET(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {

    }

    override func POST(URLString: String, parameters: AnyObject?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {

    }
    */

    // 把get和post集合成了一个
    func request(type: ClientType, URLString: String, parameters: AnyObject?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {

        if type == .GET {
            self.GET(URLString, parameters: parameters, success: success, failure: failure)

        } else if type == .POST {
            self.POST(URLString, parameters: parameters, success: success, failure: failure)
        }
    }


//    // 去掉 progress (通过方法重载, 来减少参数, 方便他人使用)
//    func request(type: ClientType, URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
//        // 调用
//        self.request(type, URLString: URLString, parameters: parameters, success: success, failure: failure)
//    }


//    // 去掉 NSURLSessionDataTask (通过方法重载, 减少闭包回调参数)
//    func request(type: ClientType, URLString: String, parameters: AnyObject?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((AnyObject?) -> Void)?, failure: ((NSError) -> Void)?) {
//
//        let success = { (task: NSURLSessionDataTask, json: AnyObject?) -> Void in
//            success?(json)
//        }
//
//        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
//            failure?(error)
//        }
//
//        if type == .GET {
//            self.GET(URLString, parameters: parameters, success: success, failure: failure)
//
//        } else if type == .POST {
//            self.POST(URLString, parameters: parameters, success: success, failure: failure)
//        }
//    }

    /// 最终抽取
    /// (对象方法) 没有进度, 也没有NSURLSessionDataTask
    func request(type: ClientType, URLString: String, parameters: AnyObject?, success: (AnyObject?) -> Void, failure: (NSError) -> Void) {

        let success = { (task: NSURLSessionDataTask, json: AnyObject?) -> Void in
            success(json)
        }

        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            failure(error)
        }

        self.request(type, URLString: URLString, parameters: parameters, progress: nil, success: success, failure: failure)
    }

    /// 类方法
    class func request(type: ClientType, URLString: String, parameters: AnyObject?, success: (AnyObject?) -> Void, failure: (NSError) -> Void) {
        // 调动对象方法
        self.shareInstance.request(type, URLString: URLString, parameters: parameters, success: success, failure: failure)
    }
}
